﻿package tools;

/**
 * 单例方式一
 * */
public final class SingletonDemo {
	private static SingletonDemo singletonDemo = null;

	// 私有构造
	private SingletonDemo() {
	}

	// 公有静态方法获取单例(这种方式在并发多的情况会出现问题)
	public static SingletonDemo newInstance() {
		if (singletonDemo == null) {
			singletonDemo = new SingletonDemo();
		}
		return singletonDemo;
	}
}

/**
 * 单例方式二
 * */
class Singleton {
	private static Singleton s = new Singleton();

	private Singleton() {
	}

	public Singleton getInstance() {
		return s;
	}
}
